home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / satellit / vstsrc / vstdib.c < prev    next >
C/C++ Source or Header  |  1995-02-02  |  8KB  |  302 lines

  1. /* %W% %E% %U% */
  2.  
  3. #include <windows.h>
  4. #include "vstdefs.h"
  5. #include "vsttype.h"
  6. #include "libxtrns.h"
  7. #include "vstdib.h"
  8.  
  9. #define PALETTESZ(p) ((((BITMAPINFOHEADER *)p)->biSize != \
  10.                       sizeof(BITMAPCOREHEADER)) ? \
  11.                       getclr((BITMAPINFOHEADER *)p) * sizeof(RGBQUAD) : 0)
  12.  
  13. HBITMAP
  14. ReadDIB(version, fname, ybottom, width, height, mdp)
  15. int version;
  16. char *fname;
  17. int ybottom;
  18. int *width, *height;
  19. mapdata_t *mdp;
  20. {
  21.     OFSTRUCT of;
  22.     BITMAPINFOHEADER bi, *bip;
  23.     HANDLE h, dibh;
  24.     int fd;
  25.  
  26.     if (version < VSTVERSION)
  27.         return NULL;
  28.  
  29. #ifdef MAPSTUB
  30.     mdp->m_version = VSTVERSION;
  31.     mdp->m_maxx = 360.0;
  32.     mdp->m_minx = 0.0;
  33.     mdp->m_maxy = 89.0;
  34.     mdp->m_miny = -89.0;
  35.     mdp->m_rfu0 = 0;
  36.  
  37.     hdc = GetDC(NULL);
  38.     hbitmap = CreateCompatibleBitmap(hdc, *width = GetSystemMetrics(SM_) -50,
  39.             *height = GetSystemMetrics(SM_)-50);
  40.     SelectObject(hdc, hbitmap);
  41.     FillRect(hdc, 0, 0, *width, *height, BDrawBrush[4]);
  42.     ReleaseDC(NULL, hdc);
  43.     *height -= ybottom;
  44.     return hdc;
  45. #endif /* STUB */
  46.  
  47.     if ((fd = (int) OpenFile(fname, &of, (UINT)OF_READ)) == -1)
  48.         return NULL;
  49.  
  50.     if ((dibh = getDIBInfo(fd, width, height)) == NULL)
  51.         return NULL;
  52.         
  53.     bi = *(BITMAPINFOHEADER *) GlobalLock (dibh);        
  54.     if (bi.biSize != sizeof (BITMAPCOREHEADER)) {
  55.         if (bi.biSizeImage == 0L)
  56.             bi.biSizeImage = ALIGNBYTE(bi.biBitCount * bi.biWidth)
  57.                                     * bi.biHeight;
  58.         if (bi.biClrUsed == 0L)
  59.             bi.biClrUsed = getclr(&bi);
  60.     }
  61.     GlobalUnlock (dibh);
  62.  
  63.     if ((h = GlobalReAlloc(dibh, bi.biSize + PALETTESZ(&bi)
  64.             + bi.biSizeImage,  GHND)) == NULL) {
  65.         GlobalFree(dibh);
  66.         _lclose((HFILE)fd);
  67.         return NULL;
  68.     }
  69.     else {
  70.         dibh = h;
  71.         bip = (void  *)GlobalLock(dibh);
  72.         Readx(fd, (char *)bip + bip->biSize + PALETTESZ(bip), bi.biSizeImage);
  73.         GlobalUnlock(dibh);
  74.     }
  75.     mdp->m_minx = mdp->m_maxx = mdp->m_miny = mdp->m_maxy = 0.0;
  76.     Readx(fd, (char *) mdp, sizeof(*mdp));
  77.     _lclose((HFILE)fd);
  78.     
  79.     return DIB2Bitmap(dibh, ybottom);
  80. }
  81.  
  82.  
  83. static HANDLE
  84. getDIBInfo(int fd, int *w, int *h)
  85. {
  86.     long      offset;
  87.     int       i, size, num_colors;
  88.     HANDLE    hBi = NULL;
  89.     BITMAPINFOHEADER   bi;
  90.     BITMAPCOREHEADER   bc;
  91.     BITMAPINFOHEADER  *bip;
  92.     BITMAPFILEHEADER   bf;
  93.     RGBQUAD  rgbq, *rgbqp;
  94.     
  95.     if (fd == -1)
  96.         return NULL;
  97.  
  98.     offset = rdBitmapHdr(fd, &bf);
  99.  
  100.     if (bf.bfType != BMPMAGIC) {
  101.         bf.bfOffBits = 0L;               
  102.         _llseek((HFILE)fd, offset, SEEK_SET);
  103.     }
  104.     if (Readx((HFILE)fd, (char *)&bi, (UINT)sizeof(bi)) != sizeof(bi))
  105.         return FALSE;
  106.  
  107.     num_colors = getclr(&bi);
  108. #ifdef _DEBUG_
  109.     diag("numcolors = %d\n", num_colors);
  110. #endif /* _DEBUG_ */
  111.  
  112.     switch (size = (int)bi.biSize) {
  113.         case sizeof (BITMAPINFOHEADER):
  114. #ifdef _DEBUG_
  115.         diag("file has BITMAPINFOHEADER\n");
  116. #endif /* _DEBUG_ */
  117.             break;
  118.  
  119.         case sizeof (BITMAPCOREHEADER):
  120. #ifdef _DEBUG_
  121.             diag("file has BITMAPCOREHEADER\n");
  122. #endif /* _DEBUG_ */
  123.             bc = *(BITMAPCOREHEADER*)&bi;
  124.             bi.biSizeImage          = 0;
  125.             bi.biXPelsPerMeter      = 0;
  126.             bi.biYPelsPerMeter      = 0;
  127.             bi.biSize               = sizeof(BITMAPINFOHEADER);
  128.             bi.biWidth              = bc.bcWidth;
  129.             bi.biHeight             = bc.bcHeight;
  130.             bi.biPlanes             = bc.bcPlanes;
  131.             bi.biBitCount           = bc.bcBitCount;
  132.             bi.biCompression        = BI_RGB;
  133.             bi.biClrUsed            = num_colors;
  134.             bi.biClrImportant       = num_colors;
  135.  
  136.             _llseek((HFILE)fd, (long)sizeof(BITMAPCOREHEADER) - sizeof (BITMAPINFOHEADER), (UINT)SEEK_CUR);
  137.             break;
  138.  
  139.         default:
  140.             /* Not a DIB! */
  141.             return NULL;
  142.     }
  143.     
  144.     if (h)  *h = bi.biHeight;
  145.     if (w)  *w = bi.biWidth;
  146.     
  147.     /*  set default values if zero */
  148.     if (bi.biSizeImage == 0) {
  149. #ifdef _DEBUG_
  150.         diag("biSizeImage is zero\n");
  151. #endif /* _DEBUG_ */
  152.         bi.biSizeImage = ALIGNBYTE((long)bi.biWidth * bi.biBitCount)
  153.                          * (bi.biHeight);
  154.     }
  155.     if (bi.biClrUsed == 0) {
  156. #ifdef _DEBUG_
  157.         diag("biClrUsed is zero\n");
  158. #endif /* _DEBUG_ */
  159.         bi.biClrUsed = getclr(&bi);
  160. #ifdef _DEBUG_
  161.         diag("biClrUsed now %d\n", bi.biClrUsed);
  162. #endif /* _DEBUG_ */
  163.     }
  164.     /* get memory for BITMAPINFO structure and colortab */
  165.  
  166.     hBi = GlobalAlloc (GHND, (long) bi.biSize + num_colors * sizeof(RGBQUAD));
  167.     if (!hBi)
  168.         return NULL;
  169.         
  170.     bip = (void  *)GlobalLock (hBi);
  171.     *bip = bi;
  172.  
  173.     rgbqp = (RGBQUAD  *)((char *)bip + bi.biSize);
  174.     
  175.     if (num_colors) {
  176.  
  177.         if (size == sizeof(BITMAPCOREHEADER)) {
  178.             
  179.             Readx(fd, (char *)rgbqp, num_colors * sizeof(RGBTRIPLE));
  180.  
  181.             for (i = 0; i < num_colors; i++) {
  182.                 rgbq.rgbReserved = (BYTE)0;
  183.                 rgbq.rgbRed = ((RGBTRIPLE  *)rgbqp)[i].rgbtRed;
  184.                 rgbq.rgbBlue = ((RGBTRIPLE  *)rgbqp)[i].rgbtBlue;
  185.                 rgbq.rgbGreen = ((RGBTRIPLE  *)rgbqp)[i].rgbtGreen;
  186.                 rgbqp[i] = rgbq;
  187.             }
  188.         }
  189.         else
  190.             Readx(fd, (char *) rgbqp, num_colors * sizeof(RGBQUAD));
  191.     }
  192.  
  193.     if (bf.bfOffBits) /* back off to start of bitmap data */
  194.         _llseek(fd, (long)offset + (long)bf.bfOffBits, (UINT)SEEK_SET);
  195.  
  196.     GlobalUnlock(hBi);
  197.     return hBi;
  198. }
  199.  
  200.  
  201. static long
  202. getclr(void  * pv)
  203. {
  204.     int cbits;
  205.  
  206.     if (((BITMAPINFOHEADER *) pv)->biSize != sizeof(BITMAPCOREHEADER)) {
  207. #ifdef _DEBUG_
  208.         diag("header not BITMAPCOREHEADER\n");
  209. #endif /* _DEBUG_ */
  210.  
  211.         if (((BITMAPINFOHEADER *) pv)->biClrUsed != 0) {
  212. #ifdef _DEBUG_
  213.             diag("number of colors used = %d\n", ((BITMAPINFOHEADER *) pv)->biClrUsed);
  214. #endif /* _DEBUG_ */
  215.             return (WORD) ((BITMAPINFOHEADER *) pv)->biClrUsed;
  216.         }
  217.         cbits = ((BITMAPINFOHEADER *) pv)->biBitCount;
  218. #ifdef _DEBUG_
  219.         diag("ClrUsed is zero, color = %d bits\n", cbits);
  220. #endif /* _DEBUG_ */
  221.     }
  222.     else {
  223.         cbits = ((BITMAPCOREHEADER *)pv)->bcBitCount;
  224. #ifdef _DEBUG_
  225.         diag("Oldstyle bitmap, color = %d bits\n", cbits);
  226. #endif /* _DEBUG_ */
  227.     }
  228.     if (cbits == 1)
  229.         return 2;
  230.     if (cbits == 4)
  231.         return 16;
  232.     if (cbits == 8)
  233.         return 256;
  234.     return 0;
  235. }
  236.  
  237. /*
  238.  * allows initialization data to be offset in Y
  239.  */
  240.  
  241. static HBITMAP
  242. DIB2Bitmap(HANDLE dibp, int ybottom)
  243. {
  244.     BITMAPINFOHEADER   *bip;
  245.     BITMAPINFOHEADER    bi;
  246.     HDC                 hdc;
  247.     HBITMAP             hbm;
  248.  
  249.     if ((bip = (void  *)GlobalLock(dibp)) == NULL)
  250.         return NULL;
  251.  
  252.     memcpy((void *)&bi, (void *)bip, sizeof(bi));
  253.     bi.biHeight += ybottom;
  254.     bi.biSizeImage = ALIGNBYTE(bi.biWidth * bi.biBitCount) * bi.biHeight;
  255.  
  256.     hdc = GetDC(NULL);
  257.     hbm = CreateDIBitmap(hdc, (BITMAPINFOHEADER *)&bi , (long)CBM_INIT ,
  258.         (char *)bip + bip->biSize + PALETTESZ(bip),
  259.         (BITMAPINFO *)bip , DIB_RGB_COLORS);
  260.  
  261.     ReleaseDC(NULL,hdc);
  262.     GlobalUnlock(dibp);
  263.     GlobalFree(dibp);
  264.     
  265.     return hbm;
  266. }
  267.  
  268. static long
  269. Readx(fd, pv, toread)
  270. int fd;
  271. void *pv;
  272. long toread;
  273. {
  274.     long  chunk, nread = 0;
  275.     char  *bufp = pv;
  276.  
  277.     while (toread > 0) {
  278.         chunk = MIN(toread, CHUNKSIZE);
  279.         if ((long)_lread((HFILE) fd, (char *) bufp, (int)chunk) != chunk)
  280.                 return nread;
  281.         nread += chunk;
  282.         toread -= chunk;
  283.         bufp += chunk;
  284.     }
  285.     return nread;
  286. }
  287.  
  288. static long
  289. rdBitmapHdr(fd, pbp)
  290. int fd;
  291. BITMAPFILEHEADER *pbp;
  292. {
  293.     long offset;
  294.  
  295.     offset = _llseek((HFILE) fd, 0L, SEEK_CUR);
  296.     _lread((HFILE)fd, (char *) &pbp->bfType, sizeof(short));
  297.     /* size is in the next *3* longs */
  298.     _lread((HFILE)fd, (char *) &pbp->bfSize, sizeof(long) * 3);
  299.     return offset;
  300. }
  301.  
  302.